package SceneUnit
{
	import BasicEngine.Dispatcher;
	import BasicEngine.EntityManager;
	import BasicEngine.MsgId;
	import BasicEngine.MsgManager;
	import DataStore.DataSource;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import Map.MapManager;

	public class TileMap extends Sprite
	{
		public var MapData:BitmapData;
		public var MapImg:Bitmap;
		public var MapWidth:int;
		public var MapHeight:int;
		public var CurrentMapThumbnail:BitmapData;
		public var CurrentMapName:String;
		public var BgGrains:Vector.<Bitmap>;
		public var DefaultMap:String;
		
		public function TileMap(_default:String)
		{
			DefaultMap = _default;
		}
		
		public function Init():void
		{
			MapImg = new Bitmap();
			Dispatcher.BeginInvoke(GetFlag, SwitchMap, 1000, DefaultMap);
			SceneManager.MapId = EntityManager.CreateEntityWithID(SceneManager.SceneID, "地图实体", 1);
			MsgManager.RegistIDProc(SceneManager.MapId, DataSource.MsgMins["Update"],Update,true);
		}
		
		public function Update(_msg:MsgId):void
		{
			
		}
		
		
		public function SwitchMap(_url:String):void
		{			
			CurrentMapThumbnail = SceneManager.Thumbnails[_url];
			CurrentMapName = _url;
			MapData = MapManager.ParseMapSt(_url);
			MapImg.bitmapData = MapData;
			addChild(MapImg);
			MapWidth = MapData.width;
			MapHeight = MapData.height;
		}
		
		public function DeActive():void
		{
			MapData.dispose();
			CurrentMapThumbnail.dispose();
			this.removeChild(MapImg);
			MapWidth = 0;
			MapHeight = 0;
			CurrentMapName = "";
		}
		
		public static function GetFlag():Boolean
		{
			return(MapManager.GetFlag() && SceneManager.GetFlag());
		}
	}
}